package com.sprucetec.live.service.impl;

import com.github.pagehelper.PageHelper;
import com.sprucetec.live.common.dto.PageDTO;
import com.sprucetec.live.common.dto.PagedListDTO;
import com.sprucetec.live.common.dto.business.LiveAnchorDto;
import com.sprucetec.live.common.webcontext.LoginUserContext;
import com.sprucetec.live.entity.LiveAnchor;
import com.sprucetec.live.entity.LiveCategory;
import com.sprucetec.live.enums.DeleteStatusEnum;
import com.sprucetec.live.mapper.LiveAnchorMapper;
import com.sprucetec.live.service.LiveAnchorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenkai
 * @date 2020/3/30 3:21 下午
 * @description:
 */
@Service("liveAnchorService")
@Slf4j
public class LiveAnchorServiceImpl implements LiveAnchorService {

    @Autowired
    private LiveAnchorMapper liveAnchorMapper;

    @Override
    public PagedListDTO<LiveAnchor> getLiveAnchorPage(LiveAnchorDto liveAnchorDto) {
        PagedListDTO<LiveAnchor> pagedListDTO = new PagedListDTO<>();
        PageHelper.startPage(liveAnchorDto.getPageIndex(), liveAnchorDto.getPageSize());

        Example example = new Example(LiveAnchor.class);
        if (StringUtils.isNotBlank(liveAnchorDto.getAnchorName())) {
            example.createCriteria().andLike("anchorName", String.valueOf(liveAnchorDto.getAnchorName()));

        }
        example.createCriteria().andEqualTo("isDeleted", DeleteStatusEnum.VALID.getCode());
        List<LiveAnchor> liveAnchorList = liveAnchorMapper.selectByExample(example);
        pagedListDTO.setRecords(liveAnchorList);
        pagedListDTO.setTotal(liveAnchorList.size());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageSize(liveAnchorDto.getPageSize());
        pageDTO.setPageIndex(liveAnchorDto.getPageIndex());
        pagedListDTO.setPage(pageDTO);
        return pagedListDTO;
    }

    @Override
    public LiveAnchor getLiveAnchorInfo(Long id) {
        return liveAnchorMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addLiveAnchor(LiveAnchor liveAnchor) {
        liveAnchor.setCreateTime(new Date());
        liveAnchor.setUpdateTime(new Date());
        liveAnchor.setCreateUserCode(LoginUserContext.getUser().getUid());
        liveAnchor.setCreateUserName(LoginUserContext.getUser().getName());
        liveAnchor.setUpdateUserCode(LoginUserContext.getUser().getUid());
        liveAnchor.setUpdateUserName(LoginUserContext.getUser().getName());
        liveAnchorMapper.insertSelective(liveAnchor);
    }

    @Override
    public void updateLiveAnchor(LiveAnchor liveAnchor) {
        liveAnchor.setUpdateTime(new Date());
        liveAnchor.setUpdateUserCode(LoginUserContext.getUser().getUid());
        liveAnchor.setUpdateUserName(LoginUserContext.getUser().getName());
        liveAnchorMapper.updateByPrimaryKeySelective(liveAnchor);
    }

    @Override
    public List<LiveAnchor> getLiveAnchorList() {
        Example example = new Example(LiveAnchor.class);
        example.createCriteria().andEqualTo("isDeleted", DeleteStatusEnum.VALID.getCode());
        return liveAnchorMapper.selectByExample(example);
    }

    @Override
    public Map<Long, LiveAnchor> getAllAnchorMap() {
        List<LiveAnchor> allAnchorList = getLiveAnchorList();
        Map<Long, LiveAnchor> anchorMap = new HashMap<>(allAnchorList.size());
        for (LiveAnchor anchor : allAnchorList) {
            anchorMap.put(anchor.getId(), anchor);
        }
        return anchorMap;
    }
}
